package com.tsingteng.cosfun.ui.focus.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.AttentRecommendbean;
import com.tsingteng.cosfun.utils.ImageUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class FocusAttentionAdapter extends BaseAdapter{
    private Context mContext;
    private List<AttentRecommendbean> mListRecommen;
    private FocusAttentListener mAttentListener;
    public FocusAttentionAdapter(Context context,List<AttentRecommendbean> listRecommen,FocusAttentListener listener){
        this.mContext = context;
        this.mListRecommen = listRecommen;
        this.mAttentListener = listener;
    }
    @Override
    public int getCount() {
        if (mListRecommen.size()%3 == 0) {
            return mListRecommen.size()/3;
        }else{
            return mListRecommen.size()/3+1;
        }
    }

    @Override
    public AttentRecommendbean getItem(int i) {
        return mListRecommen.get(i);
    }

    @Override
    public long getItemId(int i) {
        return mListRecommen.size();
    }

    @Override
    public View getView(int positon, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view ==null){
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_focusopus_layout,viewGroup,false);
            viewHolder.mFoucsOne = view.findViewById(R.id.focus_iv);
            viewHolder.mFoucsName = view.findViewById(R.id.focus_name);
            viewHolder.mFoucsTv = view.findViewById(R.id.focus_tv);
            viewHolder.mFoucsTwoIv = view.findViewById(R.id.focus_two_iv);
            viewHolder.mFoucsTwoName = view.findViewById(R.id.focus_two_names);
            viewHolder.mFoucsTwoTv = view.findViewById(R.id.focus_two_tv);
            viewHolder.mFoucsThreeIv = view.findViewById(R.id.focus_three_iv);
            viewHolder.mFocusThreeName = view.findViewById(R.id.focus_three_name);
            viewHolder.mFocusThreeTv = view.findViewById(R.id.focus_three_tv);
            viewHolder.line=view.findViewById(R.id.line_ll);
            viewHolder.lin_foucs_one=view.findViewById(R.id.lin_foucs_one);
            viewHolder.lin_foucs_two=view.findViewById(R.id.lin_foucs_two);
            viewHolder.lin_foucs_three=view.findViewById(R.id.lin_foucs_three);

            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)view.getTag();
        }
        if (positon*3 < mListRecommen.size()) {
            AttentRecommendbean atten = mListRecommen.get(positon * 3);
            viewHolder.lin_foucs_one.setVisibility(View.VISIBLE);
            ImageUtils.LoadImage(mContext, atten.profileAvatar, viewHolder.mFoucsOne);
            viewHolder.mFoucsName.setText(atten.profileName);
            if (1 == atten.isAttention){
                viewHolder.mFoucsTv.setText(mContext.getResources().getString(R.string.focused));
            }else{
                viewHolder.mFoucsTv.setText(mContext.getResources().getString(R.string.focus));
            }
            viewHolder.mFoucsTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mAttentListener.attentFocus(positon * 3,atten);
                }
            });
            viewHolder.mFoucsOne.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mAttentListener.clickimage(positon*3,atten);
                }
            });
        }else{
            viewHolder.lin_foucs_one.setVisibility(View.INVISIBLE);
        }
        if (positon*3+1 < mListRecommen.size()){
            viewHolder.lin_foucs_two.setVisibility(View.VISIBLE);
            AttentRecommendbean atten = mListRecommen.get(positon*3+1);
            ImageUtils.LoadImage(mContext,atten.profileAvatar,viewHolder.mFoucsTwoIv);
            viewHolder.mFoucsTwoName.setText(atten.profileName);
            if (1 == atten.isAttention){
                viewHolder.mFoucsTwoTv.setText(mContext.getResources().getString(R.string.focused));
            }else{
                viewHolder.mFoucsTwoTv.setText(mContext.getResources().getString(R.string.focus));
            }
            viewHolder.mFoucsTwoTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   mAttentListener.attentFocus(positon*3+1,atten);
                }
            });
            viewHolder.mFoucsTwoIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mAttentListener.clickimage(positon*3+1,atten);
                }
            });
        }else{
            viewHolder.lin_foucs_two.setVisibility(View.INVISIBLE);
        }
        if (positon*3+2< mListRecommen.size()) {
            AttentRecommendbean atten = mListRecommen.get(positon*3+2);
            viewHolder.lin_foucs_three.setVisibility(View.VISIBLE);
            ImageUtils.LoadImage(mContext,atten.profileAvatar,viewHolder.mFoucsThreeIv);
            viewHolder.mFocusThreeName.setText(atten.profileName);
            if (1 == atten.isAttention){
                viewHolder.mFocusThreeTv.setText(mContext.getResources().getString(R.string.focused));
            }else{
                viewHolder.mFocusThreeTv.setText(mContext.getResources().getString(R.string.focus));
            }
            viewHolder.mFocusThreeTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   mAttentListener.attentFocus(positon*3+2,atten);
                }
            });
            viewHolder.mFoucsThreeIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mAttentListener.clickimage(positon*3+2,atten);
                }
            });
        }else{
            viewHolder.lin_foucs_three.setVisibility(View.INVISIBLE);
        }
        return view;
    }
    class ViewHolder{
        LinearLayout line;
        LinearLayout lin_foucs_one;
        LinearLayout lin_foucs_two;
        LinearLayout lin_foucs_three;
        CircleImageView mFoucsOne;
        TextView mFoucsName;
        TextView mFoucsTv;
        CircleImageView mFoucsTwoIv;
        TextView mFoucsTwoName;
        TextView mFoucsTwoTv;
        CircleImageView mFoucsThreeIv;
        TextView mFocusThreeName;
        TextView mFocusThreeTv;
    }
   public interface  FocusAttentListener{
        void attentFocus(int position,AttentRecommendbean bean);
        void clickimage(int position,AttentRecommendbean bean);
    }
}
